// JavaScript Document
(function($){
	$.fn.imagemenu = function(options){
		var settings = { 			
			openWidth: 550,
			totalSteps: 5,
			timeStep: 100
		}; 		
		var clW, cW, nrW; //various widths
		var aL, Menu; //jQuery objects
		var opened, ct;		
		$.fn.imagemenu.slide = function(initState){			
			nW = cW;
			sO = aL.eq(opened);	
			if(initState){					
				aL.each(function(i){$(this).width(nrW);});
			}else{									
				if( sO.width() < settings.openWidth ){
					aL.each(function(i){
						if(i != opened){						
							pW = Math.round(parseInt(this.style.width, '10'));					
							if(pW > clW){
								dW = Math.round((pW - clW)/settings.totalSteps ); 
								dW = dW < 0 ? 0 : dW;
								this.style.width = (pW - dW) + 'px';			
								nW -= (pW - dW);
							}
						}
					});										
					sO.width(nW);
				}else{clearInterval(ct);}			
			}			
		}			
		return this.each(function(){
			Menu = $(this);
			aL = Menu.children('li.rollable a');
			cW = Menu.width();
			nrW = Math.round( cW / aL.length ); //80 - in example
			clW = Math.round(( cW - settings.openWidth ) / ( aL.length - 1 ));	 //40 - inexample	
			aL.each(function(i){
				$(this).width(nrW).hover(function(){					
						clearInterval(ct); opened = i;
						ct=setInterval(function(){$.fn.imagemenu.slide(false);},settings.timeStep);
					}, function(){
						clearInterval(ct);
						ct=setInterval(function(){$.fn.imagemenu.slide(true);},settings.timeStep);
					}
				);	
			});
		});	
	};
})(jQuery);